﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace canvasTest
{
    public partial class Connection : UserControl
    {
        #region 依赖属性
        public static readonly DependencyProperty StartPointProperty =
            DependencyProperty.Register("StartPoint", typeof(Point), typeof(Connection),
                new FrameworkPropertyMetadata(default(Point),
                    FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty EndPointProperty =
            DependencyProperty.Register("EndPoint", typeof(Point), typeof(Connection),
                new FrameworkPropertyMetadata(default(Point),
                    FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty SourceBlockProperty =
            DependencyProperty.Register("SourceBlock", typeof(BlockDesign), typeof(Connection));

        public static readonly DependencyProperty TargetBlockProperty =
            DependencyProperty.Register("TargetBlock", typeof(BlockDesign), typeof(Connection));
        #endregion

        #region 属性访问器
        public Point StartPoint
        {
            get => (Point)GetValue(StartPointProperty);
            set => SetValue(StartPointProperty, value);
        }

        public Point EndPoint
        {
            get => (Point)GetValue(EndPointProperty);
            set => SetValue(EndPointProperty, value);
        }

        public BlockDesign SourceBlock
        {
            get => (BlockDesign)GetValue(SourceBlockProperty);
            set => SetValue(SourceBlockProperty, value);
        }

        public BlockDesign TargetBlock
        {
            get => (BlockDesign)GetValue(TargetBlockProperty);
            set => SetValue(TargetBlockProperty, value);
        }

        public PathGeometry PathGeometry
        {
            get
            {
                var geometry = new PathGeometry();
                var figure = new PathFigure { StartPoint = StartPoint };

                // 修正：移除 IsSmooth 属性
                var controlOffset = CalculateControlOffset();
                var segment = new BezierSegment
                {
                    Point1 = new Point(StartPoint.X + controlOffset, StartPoint.Y),
                    Point2 = new Point(EndPoint.X - controlOffset, EndPoint.Y),
                    Point3 = EndPoint
                };

                figure.Segments.Add(segment);
                geometry.Figures.Add(figure);
                return geometry;
            }
        }
        #endregion

        public Connection()
        {
            InitializeComponent();
            DataContext = this;
            Loaded += (s, e) => UpdateConnection();
        }

        #region 核心逻辑
        private double CalculateControlOffset()
        {
            var dx = EndPoint.X - StartPoint.X;
            return dx * 0.4;
        }

        // 修改以下两处引用
        public void UpdateConnection()
        {
            if (SourceBlock != null && TargetBlock != null)
            {
                StartPoint = SourceBlock.OutputConnectorPosition;  // 使用正确属性名
                EndPoint = TargetBlock.InputConnectorPosition;     // 使用正确属性名
            }
            InvalidateVisual();
        }
        #endregion

        #region 渲染修正（箭头绘制优化）
        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);

            if (PathGeometry.Figures.Count > 0 &&
                PathGeometry.Figures[0].Segments.Count > 0)
            {
                var endPoint = ((BezierSegment)PathGeometry.Figures[0].Segments[0]).Point3;
                DrawArrowhead(dc, endPoint);
            }
        }

        private void DrawArrowhead(DrawingContext dc, Point endPoint)
        {
            // 优化箭头方向计算
            Vector direction = EndPoint - StartPoint;
            if (direction.Length > 0)
            {
                direction.Normalize();
            }
            else
            {
                direction = new Vector(1, 0); // 默认向右
            }

            const double arrowSize = 8;
            var arrowPen = new Pen(Brushes.DarkGray, 2);

            // 修正箭头坐标计算
            Point arrowTip = endPoint;
            Point arrowLeft = arrowTip - direction * arrowSize + new Vector(-arrowSize / 2, arrowSize / 2);
            Point arrowRight = arrowTip - direction * arrowSize + new Vector(arrowSize / 2, arrowSize / 2);

            var arrowGeometry = new PathGeometry();
            var figure = new PathFigure
            {
                StartPoint = arrowTip,
                IsClosed = true
            };
            figure.Segments.Add(new LineSegment(arrowLeft, true));
            figure.Segments.Add(new LineSegment(arrowRight, true));
            arrowGeometry.Figures.Add(figure);

            dc.DrawGeometry(Brushes.DarkGray, arrowPen, arrowGeometry);
        }
        #endregion
    }
}